<html>
<head><title>Test results</title>
<script src="jspon.js"></script>
<script src="jsonschema.js"></script>
<script>
var simpleObj = {
  "name" : "John Doe",
  "age" : 30,
  "schema" : {
    "name": {"type":"string",
       "required":true},
    "age" : {"type":"number",
      "maximum":125}
  }
}
var biggerObj = {
  "name" : "John Doe",
  "born" : "1979-03-23T06:26:07Z",
  "gender" : "mal",
  "address" : {"street":"123 S Main St",
    "city":33,//"Springfield",
    "state":"CA"}
  
}
var biggerSchema = {
    "name": {"type":"string",
	length:5,
      "required":true},
	  	"final":true,
    "born" : {"type":["number","string"], //allow for a numeric year, or a full date
      "format":"date", //format when a string value is used
      "minimum":1900, //min/max for when a number value is used
      "maximum":2010
    },
    "gender" : {"type":"string",
    "options":["male","female"]},
    "address" : {"type":
        {"street":{"type":"string"},
         "city":{"type":"string"},
         "state":{"type":"string"}},
       "format":"address"}
  }
 var schemaForSchema =
{
 "id":"schemaDef",
 "final":
  {"type":"boolean",
   "description":"This indicates that the instance objects should not have any additional properties beyond what is defined in the schema object, and the schema object should not be extended. This is false by default."},
 "*":
   {
   "id":"propDef",
   "type":{
     "type":{
     	"type":["string",
		{"id":"schemaDef"},
		{"items":
		  {"type":
		    ["string",{"id":"schemaDef"}]
		  },
		}],
	"description":"This is a type definition value. A type definition can be a string indicating a primitive, it can be another schema definition object, or it can be an array which is a union of type definitions. The following are acceptable strings to denote primitive values:\n\nstring - Value must be a string. \nnumber - Value must be a number. \nboolean - Value must be a boolean. \nobject - Value must be an object. Note this is equivalent to \"type\":{}, which is a type referring to an empty schema which allows any object. \narray - Value must be an array. Note this is equivalent to \"type\":{\"items\":{}}, which is a type referring to an schema which allows arrays of any values. \nany - Value may be of any type. This does not affect null values, a null value is only acceptable if the \"nullable\" property is true. Note this is equivalent to not defining the type property in the property definition. \nIf the property is not defined or is not in this list, than any type of value is acceptable. Other type values may be used for custom purposes, but minimal validators of the specification implementation can allow any instance value on unknown type values.",
	"default":"any"},
     "required":{
     	"type":"boolean",
     	"description":"This indicates that the instance property in the instance object is required. This is false by default.",
	"default":false},
     "nullable":{
     	"type":"boolean",
     	"description":"This indicates that the instance property allows null. This is false by default.",
	"default":false},
     "unique":{
     	"type":"boolean",
     	"description":"This indicates that the instance property should have unique values. No other property with the same name in the instance object tree should have the same value.",
	"default":false},
     "minimum":{
     	"type":"number",
     	"description":"This indicates the minimum value for the instance property when the type of the instance value is a number, or it indicates the minimum number of values in an array when an array is the instance value."},
     "maximum":{
     	"type":"number",
     	"description":"This indicates the maximum value for the instance property when the type of the instance value is a number, or it indicates the maximum number of values in an array when an array is the instance value."},
     "pattern":{
     	"type":"string",
     	"format":"regex",
     	"description":"When the instance value is a string, this provides a regular expression that a instance string value should match in order to be valid.",
	"default":".*"},
     "length" :{
     	"type":"number",
     	"description":"When the instance value is a string, this indicates maximum length of the string."},
     "options" : {
     	"type":{"items":{}},
     	"description":"This provides an enumeration of possible values that are valid for the instance property."},
     "unconstrained":{
     	"type":"boolean",
     	"description":"When used in conjunction with the options property, this indicates a value can be used that is not in the list of options. This has no meaning when the options property is not a sibling.",
	"default":false},
     "readonly":{
     	"type":"boolean",
     	"description":"This indicates that the instance property should not be changed (this is only for interaction, it has no effect for standalone validation).",
	"default":false},
     "description":{
     	"type":["string",
     		{"*":{"type":"string"}}],
     	"description":"This provides a description of the purpose the instance property. The value can be a string or it can be an object with properties corresponding to various different instance languages (with an optional default property indicating the default description)."},
     "format":{
     	"type":"string",
     	"description":"This indicates what format the data is among some predefined formats which may include:\n\ndate - a string following the ISO format \naddress \nschema - a schema definition object \nperson \npage \nhtml - a string representing HTML"},
     "default":{
     	"type":"any",
     	"description":"This indicates the default for the instance property."},
     "transient":{
     	"type":"boolean",
     	"description":"This indicates that the property will be used for transient/volatile values that should not be persisted. This is false by default.",
	"default":false}
     },
     "description":"This provides a default property definition for all properties that are not explicitly defined in the schema object.",
   },
 "items":{"id":"propDef"}
}
 
 window.onload = function() {
 	schemaForSchema = JSPON.intake(schemaForSchema); // resolve references
	showResults("schemaForSchema", JSONSchema.validate(schemaForSchema,schemaForSchema));
	showResults("biggerSchema", JSONSchema.validate(biggerSchema,schemaForSchema));
	showResults("simpleObj", JSONSchema.validate(simpleObj));
	showResults("biggerObj", JSONSchema.validate(biggerObj,biggerSchema));
	function showResults(testName, results) {
		if (results.length) { 
			resultsString = "Errors:";
			for (var i = 0; i < results.length; i++)
				resultsString += "<br/>Validation error for: " + results[i].property + " message: " + results[i].message
		 }		
		else
			resultsString = "Valid";
			document.body.appendChild(document.createElement('div')).innerHTML = testName + ": " + resultsString;
	}
}
</script>
<body>
<h2>Test results</h2>


</body></html>
